package com.zixi.beverage_order.controller.seller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zixi.beverage_order.mapper.SellerMapper;
import com.zixi.beverage_order.pojo.Seller;
import com.zixi.beverage_order.service.seller.SignUpInService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/seller")
public class SellerController {

    @Autowired
    SignUpInService signUpInService;
    @Autowired
    SellerMapper sellerMapper;

    @RequestMapping("/unauthorized")
    @ResponseBody
    public String Unauthorized() {
        return "未授权,禁止访问!";
    }

    @PostMapping("/login")
    public String login(@RequestParam("phonenumber") String phonenumber,
                        @RequestParam("password") String password,
                        Model model) {
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(phonenumber, password);

        try {
            subject.login(usernamePasswordToken);
            return "system";
        } catch (
                UnknownAccountException e) {//用户名不存在
            model.addAttribute("errmsg", "用户名错误！");
            return "index";
        } catch (
                IncorrectCredentialsException e) {//密码不存在
            model.addAttribute("errmsg", "密码错误！");
            return "index";
        }

    }

    @PostMapping("/signup")
    public String signup(@RequestParam("username") String username,
                         @RequestParam("phonenumber") String phonenumber,
                         @RequestParam("password") String password,
                         Model model) {
        Seller sellerSignup = new Seller();
        sellerSignup.setUsername(username);
        sellerSignup.setPhonenumber(phonenumber);
        sellerSignup.setPassword(password);
        sellerSignup = signUpInService.signup(sellerSignup);
        if (sellerSignup.getDeleted()) {
            //返回的对象标记是已删除，说明存在此电话号码
            model.addAttribute("msg", "电话号码已被使用！");
            return "signup";
        } else {
            model.addAttribute("msg", "注册成功！");
            return "redirect:/index";
        }
    }

    @RequestMapping("/logout")
    public String logout() {
        SecurityUtils.getSubject().logout();
        return "index";
    }

    @GetMapping("/update")
    public String update(){
        return "sellerinfo/sellerinfoupdate";
    }

    @PostMapping("/updateinfo")
    public String updateInfo(@RequestParam("oldphonenumber") String oldphonenumber,
                  @RequestParam("username") String username,
                  @RequestParam("newphonenumber") String newphonenumber,
                  @RequestParam("password") String password) {
        Seller seller = new Seller(sellerMapper.selectOne(new QueryWrapper<Seller>().eq("phonenumber", oldphonenumber)).getId(),
                newphonenumber, username, password.isEmpty()?null:password, null, null, null);
        signUpInService.updateInfo(seller);
        return "system";
    }

}

